package fr.aprr.odyssee.server.domain.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import fr.aprr.odyssee.server.domain.entities.Odyssee;
import fr.aprr.odyssee.server.domain.entities.VehiculeFamille;


@Repository
public class OngletDAO {
	
	
	@Autowired
	@Qualifier("sessionFactory")
	SessionFactory sessionFactory;
	
	@Transactional
	public VehiculeFamille get(Long i) {
		Session session = sessionFactory.openSession();
		return (VehiculeFamille) session.load(VehiculeFamille.class, i);
		
	}

	@Transactional(readOnly = false)
	public void addOnglet(Odyssee p) {
		Session session = sessionFactory.openSession();
		session.saveOrUpdate(p);
		session.flush();
		System.out.println("Add new object " + p.getNomChamp());
	}
	
	@Transactional
	public List<Odyssee> getAllArticle(){
		
		List<Odyssee> art = null;
		
		Session session = sessionFactory.openSession();
		
		session.beginTransaction();
		
		try {
			
			art = session.createCriteria(Odyssee.class).list();
			session.getTransaction().commit();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			session.getTransaction().rollback();
		}
		finally {
			session.close();
		}
		
		System.out.println("List : " + art.size());
		return art;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
}
